package com.dbh.company.freeform.sensitive;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import com.dbh.company.econtract.BatchUpdateParam;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

/**
 * @author dongbanghui
 * @create 2023/7/6
 */
public class BatchUpdateDigest {

    public static void main(String[] args) throws Exception {
        TimeInterval timer = DateUtil.timer();
        String domain = "http://freeform.lianjia.com";
        long startId = 6300 * 10000L, endId =  6500 * 10000L;
        long totalCount = endId - startId;
        double everyFlushCount = 500D;
        int count = Convert.toInt(Math.ceil(totalCount / everyFlushCount));
        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(count);
        ExecutorService executorService = ThreadUtil.newExecutor(4, 4, 100 * 10000);
        for (int i = 0; i < count; i++) {
            long updateStartId = startId + Convert.toLong(everyFlushCount) * i + 1;
            Long updateEndId = Math.min(updateStartId + Convert.toLong(everyFlushCount), endId);
            executorService.execute(() -> {
                BatchUpdateParam batchUpdateParam = new BatchUpdateParam(updateStartId, updateEndId);
                String result = RemoteOperate.batchUpdateCfAttachmentThirdRelation(batchUpdateParam, domain);
                Console.log(result);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        Console.log("刷新数据条数:{},花费分钟:{}", (endId - startId + 1), timer.intervalMinute());
    }
}
